package com.sunfeng.two.p1;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;
import util.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Auther: Administrator
 * @Date: 2019/2/26 10:43
 * @Description:
 */
public class Task01 {

    private static final String EXCHANGE_NAME = "exchange_hello";
    private static final String ROUTING_KEY = "routingkey_hello";
    private static final String QUEUE_NAME = "hello";


    public static void main(String[] args) throws IOException, TimeoutException {
        final Channel channel = ConnectionUtil.getChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, false, null);

        channel.queueDeclare(QUEUE_NAME, true, false, false, null);

        //将交换器与队列通过路由键绑定   这里是绑定键
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
        //发送一条持久化的消息: hello world !
        String message = "Hello World!";

        for (int i = 0; i < 100; i++) {
            channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, MessageProperties.PERSISTENT_TEXT_PLAIN, (i+message).getBytes());

        }
        System.out.println("消息发送成功");

        channel.close();


    }
}
